package com.song.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;


/**
 * 扩充SpringMVC配置：dispatcherServlet
 *      1.视图跳转
 *      2.路径拦截：资源放行(登录接口、静态资源...）
 *      3.自定义组件注册：国际化
 */
@Configuration //声明我的扩充配置
//@EnableWebMvc //开启委派MVC的配置，不要轻易开启
public class MyMvcConfig implements WebMvcConfigurer {

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("index");
        registry.addViewController("/index.html").setViewName("index");
        registry.addViewController("/main.html").setViewName("dashboard");
    }

    //注册Bean自定义的国际化组件就生效了
    @Bean
    public LocaleResolver localeResolver() {
        return new MyLocaleResolver();
    }


    //注册Bean自定义添加拦截器  拦截所有的路径中排除一些静态资源、公共资源，登录页面等访问路径，否则界面样式没有了、登录不了了
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new MyLoginHandlerInterceptor())
                .addPathPatterns("/**").excludePathPatterns("index.html","/","/user/login","/asserts/**");
    }
}
